//
//  ViewController.m
//  2048_Demo
//
//  Created by SongLian on 16/8/7.
//  Copyright © 2016年 SongLian. All rights reserved.
//

#import "ViewController.h"
#import "Helper2048.h"


@interface ViewController ()

@property (strong, nonatomic) NSMutableSet *set;
@property (weak, nonatomic) UIView *bigView;
@property (strong, nonatomic) Helper2048 *helper;

@end

@implementation ViewController

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(action_button) name:@"addLabel" object:nil];
    
    
    
    self.helper = [[Helper2048 alloc] init];
    
    
    
    
    
    self.set = [NSMutableSet set];
    NSArray *array = @[@(UISwipeGestureRecognizerDirectionLeft),@(UISwipeGestureRecognizerDirectionRight),@(UISwipeGestureRecognizerDirectionUp),@(UISwipeGestureRecognizerDirectionDown)];
    for (NSInteger i = 0; i < 4; i++) {
        UISwipeGestureRecognizer *gesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(action_gesture:)];
        [self.view addGestureRecognizer:gesture];
        gesture.direction = [array[i] integerValue];
    }

    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0, 520, 340, 40);
    button.sl_centerX = 375/2.0;
    button.backgroundColor = SLColor_Black;
    [self.view addSubview:button];
    [button addTarget:self action:@selector(action_button) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
    button1.frame = CGRectMake(0, 570, 340, 40);
    button1.sl_centerX = 375/2.0;
    button1.backgroundColor = SLColor_Black;
    [self.view addSubview:button1];
    [button1 addTarget:self action:@selector(action_button1) forControlEvents:UIControlEventTouchUpInside];
    
    [self configBigView];
}

- (void)action_gesture:(UISwipeGestureRecognizer *)gesture {
    [self.helper moveLabelWithDirection:gesture.direction];

}

- (void)action_button1 {
    

    for (SLLabel *label in self.helper.set) {
        SLLog(@"----------------section:%ld,row:%ld************", label.indexPath.section, label.indexPath.row);
    }
}


- (void)action_button {
    
    SLLabel *label = [self.helper createLabel];
    [self.bigView addSubview:label];

}

- (NSString *)obtain_2_or_4 {
    NSArray *array = @[@"2",@"4"];
    NSInteger random = arc4random()%2;
    return array[random];
}

- (NSIndexPath *)obtainIndexPath {
    NSInteger location = arc4random()%16;
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:location%4 inSection:location/4];
    return indexPath;
}

- (NSInteger)obtainLocation:(NSIndexPath *)indexpath {
    return indexpath.section * 4 + indexpath.row;
}

- (void)configBigView {
    
    UIView *bigView = [UIView sl_viewWithFrame:CGRectMake(0, 0, 340, 340) backgroudColor:SLColor_RGB(187, 173, 160)];
    bigView.center = SLPoint_ScreenCenter;
    [self.view addSubview:bigView];
    self.bigView = bigView;
    
    for (NSInteger i = 0; i < 4; i++) {
        for (NSInteger j = 0; j < 4; j++) {
            UIView *litteView = [UIView sl_viewWithFrame:CGRectMake(i*(70+12)+12, j*(70+12)+12, 70, 70) backgroudColor:SLColor_RGB(204, 192, 180)];
            [bigView addSubview:litteView];
        }
    }
}



//- (void)action_button {
//    
//    
//    //    if (self.set.count < 16) {
//    //        NSIndexPath *indexPath = [self obtainIndexPath];
//    //        if ([self.set containsObject:indexPath])
//    //            [self action_button];
//    //        else {
//    //            [self.set addObject:indexPath];
//    //
//    //            UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(indexPath.row*(70+12)+12, indexPath.section*(70+12)+12, 70, 70)];
//    //            label.backgroundColor = SLColor_RGB(238, 228, 218);
//    //            label.text = [self obtain_2_or_4];
//    //            label.textAlignment = NSTextAlignmentCenter;
//    //            label.font = [UIFont systemFontOfSize:40];
//    //            label.tag = 100 + indexPath.section * 4 + indexPath.row;
//    //            [self.bigView addSubview:label];
//    //            
//    //        }
//    //    }
//}


//- (void)action_gesture:(UISwipeGestureRecognizer *)gesture {
//    SLLog(@"%ld", gesture.direction);
//    
//    
//    switch (gesture.direction) {
//        case UISwipeGestureRecognizerDirectionRight:{
//            SLLog(@"右");
//            
//            [self.set enumerateObjectsUsingBlock:^(NSIndexPath * _Nonnull indexPath, BOOL * _Nonnull stop) {
//                if (indexPath.row != 3) {
//                    NSInteger location = [self obtainLocation:indexPath];
//                    UILabel *label = [self.view viewWithTag:location+100];
//                    [UIView animateWithDuration:kAnimationTimeInterval animations:^{
//                        label.frame = CGRectMake(3*(70+12)+12, indexPath.section*(70+12)+12, 70, 70);
//                        label.tag = 100 + indexPath.section * 4 + 3;
//                        [self.set removeObject:indexPath];
//                        [self.set addObject:[NSIndexPath indexPathForRow:3 inSection:indexPath.section]];
//                    }];
//                }
//            }];
//            
//            break;
//        }
//        case UISwipeGestureRecognizerDirectionLeft:{
//            SLLog(@"左");
//            [self.set enumerateObjectsUsingBlock:^(NSIndexPath * _Nonnull indexPath, BOOL * _Nonnull stop) {
//                if (indexPath.row != 0) {
//                    NSInteger location = [self obtainLocation:indexPath];
//                    UILabel *label = [self.view viewWithTag:location+100];
//                    SLLog(@"%@", label);
//                    [UIView animateWithDuration:kAnimationTimeInterval animations:^{
//                        label.frame = CGRectMake(0*(70+12)+12, indexPath.section*(70+12)+12, 70, 70);
//                        label.tag = 100 + indexPath.section * 4 + 0;
//                        [self.set removeObject:indexPath];
//                        [self.set addObject:[NSIndexPath indexPathForRow:0 inSection:indexPath.section]];
//                    }];
//                }
//            }];
//            break;
//        }
//        case UISwipeGestureRecognizerDirectionUp:{
//            SLLog(@"上");
//            [self.set enumerateObjectsUsingBlock:^(NSIndexPath * _Nonnull indexPath, BOOL * _Nonnull stop) {
//                if (indexPath.section != 0) {
//                    NSInteger location = [self obtainLocation:indexPath];
//                    UILabel *label = [self.view viewWithTag:location+100];
//                    SLLog(@"%@", label);
//                    [UIView animateWithDuration:kAnimationTimeInterval animations:^{
//                        label.frame = CGRectMake(indexPath.row*(70+12)+12, 0*(70+12)+12, 70, 70);
//                        label.tag = 100 + 0 * 4 + indexPath.row;
//                        [self.set removeObject:indexPath];
//                        [self.set addObject:[NSIndexPath indexPathForRow:indexPath.row inSection:0]];
//                    }];
//                }
//            }];
//            break;
//        }
//        case UISwipeGestureRecognizerDirectionDown:{
//            SLLog(@"下");
//            [self.set enumerateObjectsUsingBlock:^(NSIndexPath * _Nonnull indexPath, BOOL * _Nonnull stop) {
//                if (indexPath.section != 3) {
//                    NSInteger location = [self obtainLocation:indexPath];
//                    UILabel *label = [self.view viewWithTag:location+100];
//                    SLLog(@"%@", label);
//                    [UIView animateWithDuration:kAnimationTimeInterval animations:^{
//                        label.frame = CGRectMake(indexPath.row*(70+12)+12, 3*(70+12)+12, 70, 70);
//                        label.tag = 100 + 3 * 4 + indexPath.row;
//                        [self.set removeObject:indexPath];
//                        [self.set addObject:[NSIndexPath indexPathForRow:indexPath.row inSection:3]];
//                    }];
//                }
//            }];
//            break;
//        }
//        default:
//            break;
//    }
//    
//    SLLog(@"%@", self.set);
//}


@end
